//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Mobile.Configuration;
using Microsoft.Practices.Mobile.EndpointCatalog.Configuration;
using Microsoft.Practices.TestUtilities;

namespace Microsoft.Practices.Mobile.EndpointCatalog.Tests
{
  //  <Endpoints>
  //  <EndpointItems>
  //    <add Name="MyHost" Address="http://default/MyHost" UserName="defaultUser" Password="4444">
  //      <NetworkItems>
  //        <add Name="Internet" Address="http://internet/MyHost" UserName="peter" Password="1234" Domain="myDomain"/>
  //        <add Name="Work" Address="http://work/MyHost" UserName="chris" Password="3333"/>
  //      </NetworkItems>
  //    </add>
  //    <add Name="NoDefault">
  //      <NetworkItems>
  //        <add Name="Internet" Address="http://internet/NoDefault" UserName="chris" Password="1234"/>
  //        <add Name="Work" Address="http://work/NoDefault" UserName="peter" Password="3333" Domain="MyDomain"/>
  //      </NetworkItems>
  //    </add>
  //  </EndpointItems>
  //</Endpoints>

	[TestClass]
	public class EndpointConfigurationFactoryFixture
	{

		[TestInitialize]
		public void InitializeConfigurationManager()
		{
			ConfigurationManager.ClearCache();
			ConfigurationManager.ConfigFileName = "App.config";
		}

		[TestMethod]
		public void GetEndpointSectionDoesntGetNull()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");
				Assert.IsNotNull(section);
			}
		}


		[TestMethod]
		public void SectionGetEndpointItemsCollectionDoesntGetNull()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");

				Assert.IsNotNull(section.EndpointItemCollection);
			}
		}

		[TestMethod]
		public void SectionGetEndpointItemsCollectionContainsSameQuantityThanAppConfig()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");

				EndpointItemElementCollection collection = section.EndpointItemCollection;

				Assert.AreEqual(2, collection.Count);
			}
		}

		[TestMethod]
		public void GetEndpointFromEndpointItemsCollectionDoesntGetNull()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");
				EndpointItemElement element = section.EndpointItemCollection.GetEndpoint("MyHost");
				EndpointItemElement element2 = section.EndpointItemCollection.GetEndpoint("NoDefault");

				Assert.IsNotNull(element);
				Assert.IsNotNull(element2);
			}
		}


		[TestMethod]
		public void EndpointsAddressInEndpointCollectionAreTheSameThanAppConfig()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");

				Assert.AreEqual("http://default/MyHost", section.EndpointItemCollection.GetEndpoint("MyHost").Address);
			}
		}

		[TestMethod]
		public void EndpointsAccountNamesInEndpointCollectionAreTheSameThanAppConfig()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");
				Assert.AreEqual("defaultCredentials", section.EndpointItemCollection.GetEndpoint("MyHost").AccountName);
			}
		}

		[TestMethod]
		public void GetNetworkCollectionFromEndpointCollectionDoesntGetNull()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");
				Assert.IsNotNull(section.EndpointItemCollection.GetEndpoint("MyHost").Networks);
			}
		}

		[TestMethod]
		public void CountOfNetworkItemsInEndpointCollectionAreTheSameThanAppConfig()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");

				Assert.AreEqual(2, section.EndpointItemCollection.GetEndpoint("MyHost").Networks.Count);
			}
		}


		[TestMethod]
		public void GetNetworkFromNetworkCollectionDoesntGetNull()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");
				NetworkElement element = section.EndpointItemCollection.GetEndpoint("MyHost").Networks.GetNetwork("Internet");

				Assert.IsNotNull(element);
			}
		}

		[TestMethod]
		public void NetworkAddressInNetworkCollectionAreTheSameThanAppConfig()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");

				Assert.AreEqual("http://internet/MyHost", section.EndpointItemCollection.GetEndpoint("MyHost").Networks.GetNetwork("Internet").Address);
				Assert.AreEqual("http://work/MyHost", section.EndpointItemCollection.GetEndpoint("MyHost").Networks.GetNetwork("Work").Address);
				
			}
		}

		[TestMethod]
		public void AccountNameInNetworkCollectionAreTheSameThanAppConfig()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				EndpointSection section = (EndpointSection)ConfigurationManager.GetSection("Endpoints");

				Assert.AreEqual("peter", section.EndpointItemCollection.GetEndpoint("MyHost").Networks.GetNetwork("Internet").AccountName);
			}
		}



		[TestMethod]
		public void LoadEndpointCatalogFromConfigurationRetrievesACatalog()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				IEndpointCatalogFactory factory = new EndpointCatalogFactory("Endpoints");
				EndpointCatalog catalog = factory.CreateCatalog() as EndpointCatalog;

				Assert.IsNotNull(catalog);
			}
		}

		[TestMethod]
		public void LoadEndpointCatalogFromConfigurationRetrievesValidCatalog()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				IEndpointCatalogFactory factory = new EndpointCatalogFactory("Endpoints");
				EndpointCatalog catalog = factory.CreateCatalog() as EndpointCatalog;

				Assert.AreEqual(2, catalog.Count);
				Assert.AreEqual("http://default/MyHost", catalog.GetAddressForEndpoint("MyHost", null));
				Assert.AreEqual("defaultCredentials", catalog.GetAccountNameForEndpoint("MyHost", null));

				Assert.AreEqual("http://internet/MyHost", catalog.GetAddressForEndpoint("MyHost", "Internet"));
				Assert.AreEqual("peter", catalog.GetAccountNameForEndpoint("MyHost", "Internet"));

				Assert.AreEqual("http://work/MyHost", catalog.GetAddressForEndpoint("MyHost", "Work"));
				Assert.AreEqual("chris", catalog.GetAccountNameForEndpoint("MyHost", "Work"));

				Assert.AreEqual("http://internet/NoDefault", catalog.GetAddressForEndpoint("NoDefault", "Internet"));
				Assert.AreEqual("chris", catalog.GetAccountNameForEndpoint("NoDefault", "Internet"));

				Assert.AreEqual("http://work/NoDefault", catalog.GetAddressForEndpoint("NoDefault", "Work"));
				Assert.AreEqual("peter", catalog.GetAccountNameForEndpoint("NoDefault", "Work"));
			}
		}

		[TestMethod]
		public void GetAddressForEndpointFromDefaultValueThatDoesntExistThrowsAnException()
		{
			using (TestResourceFile resFile = new TestResourceFile(this, @"App.config"))
			{
				IEndpointCatalogFactory factory = new EndpointCatalogFactory("Endpoints");
				EndpointCatalog catalog = factory.CreateCatalog() as EndpointCatalog;

				ExtendedAssert.Throws<KeyNotFoundException>(
						delegate { catalog.GetAddressForEndpoint("NoDefault", null); });
			}
		}
	}
}
